<?php
    /**
     * interviewAction
     * 面试控制层
     *
     * @uses 
     * @package 
     * @version 
     * @copyright 2009-2011 yy
     * @author yinyi <sampeng87@gmail.com> yy<wxm201411@163.com> 
     * @license PHP Version 5.2 {@link www.sampeng.cn}
     */
class IndexAction extends Action{
	private $interview;				//题目模型
	private $interviewAnswers;		//答案模型
	private $interviewRecord;		//答题记录
	private $interviewScores;		//答题分数
	
	/**
	 * 初始化函数
	 *
	 */	
	function _initialize(){
		//参数转义
        new_addslashes($_POST);
        new_addslashes($_GET);
		
        //整个应用的赋值
		$this->interview = D('Interview');
		$this->interviewAnswers = D('InterviewAnswers');
		$this->interviewRecord = D('InterviewRecord');
		$this->interviewScores = D('InterviewScores');
		$this->interview->setInterviewAnswers($this->interviewAnswers);
        
 		$config = D('AppConfig')->getConfig();
		$this->assign('config',$config);            
	}
			
	/**
	 * 好友的成绩
	 *
	 */
	function index() {
		$friendsArray = $this->api->friend_get();
		$friends = $this->arrayToSql($friendsArray);
		$list = $this->interviewRecord->friendRecord($friends);
		$friendsOrderList = $this->friendsOrder();
		$this->assign("friendsOrderList",$friendsOrderList);
		$this->assign("allOrderList",$this->allOrder());
		$this->assign("list",$list);
		$this->display();
	}
	
	/**
	 * 我的成绩
	 */
	function myExam(){
		$uid = $this->uid;
		$list = $this->interviewScores->where("uid = ".$this->uid)->find();
		$pidsArray = $this->interviewRecord->wrongRecord($this->uid);
		$pids = $this->arrayToSql($pidsArray);
		$wrongRecordList = $this->interview->getInterviewByIds($pids);
		$this->assign("uid",$uid);
		$this->assign("wrongRecordList",$wrongRecordList);
		$this->assign("allOrderList",$this->allOrder());
		$this->assign("friendsOrderList",$this->friendsOrder());
		if($list != null){
			$this->assign("list",$list);
		}
		$this->display();
	}
	
	/**
	 * 大家的成绩
	 */
	function allExam(){
		$this->assign("friendsOrderList",$this->friendsOrder());
		$this->assign("allOrderList",$this->allOrder());
		$this->assign("uid",$this->uid);
		$list = $this->interviewRecord->allRecord();
		$this->assign("list",$list);
		$this->display();
	}
	
	/**
	 * 所有人的排行榜
	 */
	function allOrder(){
		return $this->interviewScores->allOrder();
	}
	
	/**
	 * 好友的排行榜
	 */
	function friendsOrder(){
		$friendsArray = $this->api->friend_get();
		$friends = $this->arrayToSql($friendsArray);
		return $this->interviewScores->friendsOrder($friends);
	}
	
	/**
	 * 做对的好友
	 */
	function doRight(){
		$pid = $_GET['pid'];
		$friendsArray = $this->api->friend_get();
		$friends = $this->arrayToSql($friendsArray);
		$list = $this->interviewRecord->doRightFriends($pid,$friends);
		if($list != null){
			$this->assign("list",$list);
		}
		$this->display();
	}
	
	/**
	 * 做错的好友
	 */
	function doWrong(){
		$pid = $_GET['pid'];
		$friendsArray = $this->api->friend_get();
		$friends = $this->arrayToSql($friendsArray);
		$list = $this->interviewRecord->doWrongFriends($pid,$friends);
		if($list != null){
			$this->assign("list",$list);	
		}
		$this->display();
	}
	
	/**
	 * 除好友外，所有做对的人
	 */
	function doRightOther(){
		$pid = $_GET['pid'];
		$friendsArray = $this->api->friend_get();
		if($friendsArray != NULL){
			$friends = $this->arrayToSql($friendsArray);
			$list = $this->interviewRecord->doWrongAll($this->uid,$pid,$friends,true);
		}else{
			$list = $this->interviewRecord->doWrongAll($this->uid,$pid,0,true);
		}
		if($list !=null){
			$this->assign("list",$list);
		}
		$this->display();
	}
	
	/**
	 * 开始考试
	 */
	function startExam(){
		//获取选择的题目数量,题目范围，题目类型
		$kind = $_POST['kind'];
		$type = $_POST['type'];
		$count=$_POST['count'];
		
		$interview = D('Interview');
		$list = $interview->findAll();
		
		$interviewList = $this->interview->getinterviewAnswers($kind,$type,$count,$this->uid);
		$this->assign('list',$interviewList);
		$this->display();
	}
	
	/**
	 * 选择考试类型
	 */
	function chooseExam(){
		$this->display();
	}
	
	/**
	 * 数组转换成适合sql查询的格式
	 */
	function arrayToSql($array){
		$sqlStr = "";
		foreach ($array as $str){
			$sqlStr = $sqlStr.",".$str;
		}
		return substr($sqlStr,1,strlen($sqlStr));
	}
	
	/**
	 * 考试完毕，提交试卷
	 */
	function endExam(){
		$submitAnswers = $_POST['submitAnswers'];
		$answers = explode(",",$submitAnswers);
		
		foreach ($answers as $answer){
			if($answer != "" || $answer != null){
				$answerInfo = explode(":",$answer);
				$jude = 0;	//回答正确与否的标识，1表示正确，0表示错误
				//对分数表进操作
				$answer_correct = $this->interview->where("id=".$answerInfo[0])->getField('answer_correct');
				//先判断是否回答正确，分别操作
				if(strtoupper($answer_correct)==strtoupper($answerInfo[1])){
					$jude = 1;
					//判断数据库里是否有该用户的记录
					$data = $this->interviewScores->where("uid = ".$this->uid)->find();
					if($data != null || $data != ""){
						$data['count_problem']+=1;
						$data['count_right']+=1;
						$data['scores']+=2;
						$this->interviewScores->save($data);
					}else{
						$data['uid'] = $this->uid;
						$data['count_problem']+=1;
						$data['count_right']+=1;
						$data['scores']+=2;
						$this->interviewScores->add($data);
					}
				}else{
					//判断数据库里是否有该用户记录
					$data = $this->interviewScores->where("uid = ".$this->uid)->find();
					if($data != null || $data != ""){
						$data['count_problem']+=1;
						$data['count_wrong']+=1;
//						$data['scores']+=2;
						$this->interviewScores->where("uid = ".$this->uid)->save($data);
					}else{
						$data['uid'] = $this->uid;
						$data['count_problem']+=1;
						$data['count_wrong']+=1;
//						$data['scores']+=2;
						$this->interviewScores->add($data);
					}
				}
				
				//对记录表进行操作
				if($answerInfo[0] != null || $answerInfo[0] != ""){
					$recordData['uid'] = $this->uid;
					$recordData['pid'] = $answerInfo[0]; 
					$recordData['answer'] = $answerInfo[1];
					$recordData['jude'] = $jude;
					$recordData['answer_time'] = date("Y-n-j h:i:s");
					$recordData['progress_flag'] = 1;		//1表示此题已做过
					$this->interviewRecord->add($recordData);
				}
			}
		}
		
		$list = $this->interviewScores->where("uid = ".$this->uid)->find();
		$this->assign("list",$list);
		$this->display();
	}
}
